/* ***********************************************************************
 * 项目名称:油耗
 * 项目组:
 * 创建时间:2017.7.11
 * 创建人:马方印
 * 用途:
 * 版本:1.0
 * 修改记录:    修改内容   修改原因   修改人   修改时间
 *               无        无       无       无
 *************************************************************************/

#import "YZRecordTableView.h"
#import "YZRecordHeaderFooterView.h"
#import "YZRecordLitreCell.h"
#import "MFYAnimationManager.h"//自定义动画
#import "YZInputOrOutputDetailViewController.h"//详情页面
#import "YZInputOrOutputGasolineModel.h"

@interface YZRecordTableView() <UITableViewDelegate, UITableViewDataSource>

@end

@implementation YZRecordTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    
    self = [super initWithFrame:frame style:style];
    
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.bounces = NO;
    }
    
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listErrorArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    YZListErrorModel *model = self.listErrorArray[section];
    
    return model.singleList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier = @"recordLitreCell";
    
    YZRecordLitreCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        
        cell = [[YZRecordLitreCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    //数据赋值
    YZListErrorModel *model = self.listErrorArray[indexPath.section];
    YZSingleListModel *singleListModel = model.singleList[indexPath.row];
    //如果是最后一条数据就隐藏多余的虚线
    if (singleListModel == model.singleList.lastObject) {
        
        [cell.bottomImageView setHidden:YES];
    }
    else{
        
        [cell.bottomImageView setHidden:NO];
    }
    
    cell.singleListModel = singleListModel;
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    static NSString *identifier = @"recordHeaderFooterView";
    
    YZRecordHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    
    if (!view) {
        
        view = [[YZRecordHeaderFooterView alloc]initWithReuseIdentifier:identifier];
    }
    
    //数据赋值
    YZListErrorModel *model = self.listErrorArray[section];
    view.type = self.type;
    view.listErrorModel = model;
    
    view.showDetailCompletionHandle = ^(YZListErrorModel *listErrorModel){
      
        //设置转场动画
        MFYAnimationManager *manager = [MFYAnimationManager shareMFYAnimationManater];
        manager.animationTime = 0.25;
        manager.presentedViewFrame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        
        YZInputOrOutputDetailViewController *detailVC = [[YZInputOrOutputDetailViewController alloc]init];
        detailVC.modalPresentationStyle = UIModalPresentationCustom;
        detailVC.transitioningDelegate = manager;
        detailVC.type = self.type;
        detailVC.listErrorModel = listErrorModel;
        [self.viewController presentViewController:detailVC animated:NO completion:nil];
    };
    
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //数据赋值
    YZListErrorModel *model = self.listErrorArray[indexPath.section];
    YZSingleListModel *singleListModel = model.singleList[indexPath.row];
    
    if ([singleListModel.isException integerValue] == globalNum1) {//有报警
        
        //设置转场动画
        MFYAnimationManager *manager = [MFYAnimationManager shareMFYAnimationManater];
        manager.animationTime = 0.25;
        manager.presentedViewFrame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        
        YZInputOrOutputDetailViewController *detailVC = [[YZInputOrOutputDetailViewController alloc]init];
        detailVC.modalPresentationStyle = UIModalPresentationCustom;
        detailVC.transitioningDelegate = manager;
        detailVC.type = self.type;
        detailVC.singleListModel = singleListModel;
        [self.viewController presentViewController:detailVC animated:NO completion:nil];
    }
}

#pragma mark - 设置显示数据
- (void)setListErrorArray:(NSArray *)listErrorArray{
    _listErrorArray = listErrorArray;
    
    [self reloadData];
}

@end
